/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ifpe.dao;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 *
 * @author Sostenes
 */
public class DaoFactory {

    private static DaoFactory instance;
    private Properties daoProperties = new Properties();
    private UsuarioDao usuarioDao;
    private GrupoDao grupoDao;
    private PessoaDao pessoaDao;

    public static synchronized DaoFactory getInstance() {
        if (instance == null) {
            instance = new DaoFactory();
        }
        return instance;
    }

    private DaoFactory() {
        try {
            daoProperties.load(DaoFactory.class.getClassLoader().getResourceAsStream("ifpe/config/dao.properties"));
        } catch (IOException ex) {
            throw new DaoException(ex);
        }
    }

    public GrupoDao getGrupoDao() {
        if (grupoDao == null) {
            grupoDao = (GrupoDao) getDaoInstance("grupoDao");
        }
        return grupoDao;
    }

    public PessoaDao getPessoaDao() {
        if (pessoaDao == null) {
            pessoaDao = (PessoaDao) getDaoInstance("pessoaDao");
        }
        return pessoaDao;
    }

    public UsuarioDao getUsuarioDao() {
        if (usuarioDao == null) {
            usuarioDao = (UsuarioDao) getDaoInstance("usuarioDao");
        }
        return usuarioDao;
    }

    private Object getDaoInstance(String property) {
        try {
            return Class.forName(daoProperties.getProperty(property)).newInstance();
        } catch (ClassNotFoundException ex) {
            throw new DaoException(ex);
        } catch (IllegalAccessException ex) {
            throw new DaoException(ex);
        } catch (InstantiationException ex) {
            throw new DaoException(ex);
        }
    }
}
